﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class vote_VoteInfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
             if ((this.Session["ecrm_email"] == null) || (this.Session["ecrm_email"].ToString() == ""))
            {
                userstatus1.Text = "你好，请 <a href=\"main.aspx\">登录</a>或 ";
                userstatus2.Text = "<a href=\"../user/user_reg.aspx\">注册</a>";
            }
            else
            {
                string sid = this.Session["ecrm_uid"].ToString();
                TUser oUser = new TUser(sid);
                oUser.Select();
                string bigname = oUser.BigName;

                userstatus1.Text = "<a href=\"../user/user_myself.aspx\">" + bigname + "的设置</a>";
                userstatus2.Text = "<a href=\"../user/user_logout.aspx\">退出</a>";
            }
        
        }
        if (Request["opration"] != null)
        {
            switch (Request["opration"].ToString())
            {
                case "getmonths":
                    GetMonths();
                    break;
                case "getdays":
                    GetDays();
                    break;
                case "save":
                    Save();
                    break;
            }
        }
    }
    public void Save()
    {
        TVote oVote = new TVote();
        oVote.Id = Guid.NewGuid().ToString();
        oVote.Title = Request["title"].ToString();
        oVote.InTime = DateTime.Now;
        oVote.Type = int.Parse(Request["type"].ToString());
        oVote.Description = Request["description"].ToString();
        if (Request["endtime"].ToString() == "true")
            oVote.EndTime = Convert.ToDateTime(Request["year"].ToString() + "-" + Request["month"].ToString() + "-" + Request["day"].ToString());
        else
            oVote.EndTime = DateTime.MaxValue;
        if (Request["authority"].ToString() == "true")
            oVote.Authority = 1;
        else
            oVote.Authority = 0;
        oVote.InMan = Session["userid"].ToString();
        oVote.Insert();
        string[] items = Request["items"].ToString().Split(',');
        foreach (string str in items)
        {
            if (str.Trim() != "")
            {
                TVoteItem oVoteItem = new TVoteItem();
                oVoteItem.Id = Guid.NewGuid().ToString();
                oVoteItem.InTime = DateTime.Now;
                oVoteItem.VoteId = oVote.Id;
                oVoteItem.Content = str;
                oVoteItem.InMan = Session["userid"].ToString();
                oVoteItem.Insert();
            }
        }
    }
    public void GetMonths()
    {
        string year = Request["year"].ToString();
        string month = Request["month"].ToString();
        string day = Request["day"].ToString();
        string months = "";
        if (year == DateTime.Today.Year.ToString())
        {
            for (int i = DateTime.Today.Month; i <= 12; i++)
            {
                months += " " + i;
            }
            months = months.Trim().Replace(' ', ',');
        }
        else
        {
            for (int i = 1; i <= 12; i++)
            {
                months += " " + i;
            }
            months = months.Trim().Replace(' ', ',');
        }
        Response.Clear();
        Response.Write(months);
        Response.Flush();
        Response.End();
    }
    public void GetDays()
    {
        string year = Request["year"].ToString();
        string month = Request["month"].ToString();
        string day = Request["day"].ToString();
        string days = "";
        if (year == DateTime.Today.Year.ToString() && month == DateTime.Today.Month.ToString())
        {
            for (DateTime d = DateTime.Today; d < Convert.ToDateTime(DateTime.Today.AddMonths(1).ToShortDateString().Substring(0, DateTime.Today.AddMonths(1).ToShortDateString().LastIndexOf('-') + 1) + "1"); d = d.AddDays(1))
            {
                days += " " + d.Day.ToString();
            }
            days = days.Trim().Replace(' ', ',');
        }
        else
        {
            DateTime td = Convert.ToDateTime(year + "-" + month + "-1");
            DateTime nd = Convert.ToDateTime(td.AddMonths(1).ToShortDateString().Substring(0, td.AddMonths(1).ToShortDateString().LastIndexOf('-') + 1) + "1");
            for (DateTime d = td; d < nd; d = d.AddDays(1))
            {
                days += " " + d.Day.ToString();
            }
            days = days.Trim().Replace(' ', ',');
        }
        Response.Clear();
        Response.Write(days);
        Response.Flush();
        Response.End();
    }
    public void Bind()
    {

        selyear.Items.Add(new ListItem(DateTime.Today.Year.ToString()));
        selyear.Items.Add(new ListItem(DateTime.Today.AddYears(1).Year.ToString()));
        for (int i = DateTime.Today.Month; i <= 12; i++)
        {
            selmonth.Items.Add(new ListItem(i.ToString()));
        }
        for (DateTime d = DateTime.Today; d < Convert.ToDateTime(DateTime.Today.AddMonths(1).ToShortDateString().Substring(0, DateTime.Today.AddMonths(1).ToShortDateString().LastIndexOf('-') + 1) + "1"); d = d.AddDays(1))
        {
            selday.Items.Add(new ListItem(d.Day.ToString()));
        }
        selyear.Value = DateTime.Today.Year.ToString();
        selmonth.Value = DateTime.Today.Month.ToString();
        selday.Value = DateTime.Today.Day.ToString();

    }
}
